package main.java.indi.zyj.c150.bfs;

import java.util.LinkedList;
import java.util.Queue;

public class _01_MinDpth {

    public int minDepth(TreeNode root) {
        if (root == null){
            return 0;
        }
        Queue<TreeNode> treeNodeQueueueue = new LinkedList<>();
        treeNodeQueueueue.offer(root);
        int dpth = 1;

        while (!treeNodeQueueueue.isEmpty()) {

            int queueSize = treeNodeQueueueue.size();
            for (int i = 0; i < queueSize; i++) {
                TreeNode currentNode = treeNodeQueueueue.poll();
                if (currentNode.left == null && currentNode.right == null) {
                    return dpth;
                }
                if (currentNode.left != null){
                    treeNodeQueueueue.offer(currentNode.left);
                }
                if (currentNode.right != null){
                    treeNodeQueueueue.offer(currentNode.right);
                }
            }
            dpth++;
        }
        return dpth;
    }


}
